package com.example.tests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static com.example.tests.GroupDataGenerator.generateRandomString;

import com.thoughtworks.xstream.XStream;

public class ContactInfoGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exists, please remove it manually: " + file);
			return;
		}
		
		List<ContactInfo> contact = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contact, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contact, file);
		} else {
			System.out.println("Unknown format" + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactInfo> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactInfo.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer  = new FileWriter(file);
		writer.write(xml);
		writer.close();	
	}

	private static void saveContactsToCsvFile(List<ContactInfo> contacts, File file) throws IOException {
		FileWriter writer  = new FileWriter(file);
		for (ContactInfo contact : contacts) {
			writer.write(contact.getFirstName() + "," + contact.getLastName() + "," + contact.getEmail() + "," + contact.getHomeNumber() +",!" + "\n");
		}
		writer.close();
	}
	
	public static List<ContactInfo> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactInfo.class);
		return (List<ContactInfo>) xstream.fromXML(file);
	}
	
	public static List<ContactInfo> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactInfo> list = new ArrayList<ContactInfo>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactInfo contact = new ContactInfo()
				.withFirstName(part[0])
				.withLastName(part[1])
				.withEmail(part[2])
				.withHomeNumber(part[3]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactInfo> generateRandomContacts(int amount) {
			List<ContactInfo> list = new ArrayList<ContactInfo>();
			for (int i = 0; i < amount; i++) {
				ContactInfo contact = new ContactInfo()
				.withFirstName(generateRandomString())
				.withLastName(generateRandomString())
				.withEmail(generateRandomString())
				.withHomeNumber(generateRandomString());
				list.add(contact);
			}
			return list;
	}

}
